package me.xplabs.fight.layer 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import me.xplabs.constant.ResType;
	import me.xplabs.engine.display.BaseDisplay;
	import me.xplabs.engine.display.BaseDisplayContainer;
	import me.xplabs.event.MMouseEvent;
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.MRoleInfo;
	import me.xplabs.role.RoleDisplay;
	import me.xplabs.utils.SimpleObjPool;
	import me.xplabs.utils.Utils;
	
	/**
	 * 角色层
	 * ...
	 * @author xiaohan
	 */
	public class RoleLayer extends BaseDisplayContainer 
	{
		private var _rolePool:SimpleObjPool;
		private var _roles:Vector.<RoleDisplay>;
		private var _frameNum:int;
		private var _frameIndex:int;
		private var _roleChilds:Vector.<BaseDisplay>;
		public function RoleLayer() 
		{
			super();
			_rolePool = new SimpleObjPool(RoleDisplay, 1, 1);
			_roles = new Vector.<RoleDisplay>();
			_roleChilds = new Vector.<BaseDisplay>();
			isSort = true;
		}
		/*override public function update(container:BitmapData):void 
		{
			//if (_frameIndex == 0) _frameNum = _numChildren / 300;
			//if (isSort) _childs.sort(sort);
			//_eventList.length = 0;
			//var len:int;
			//_frameIndex++;
			//if (_frameNum >= 1) len = Math.min(_numChildren, _frameIndex * 300);
			//else len = _numChildren;
			//for (var i:int = (_frameIndex - 1) * 300; i < _numChildren; i++)
			//{
				//_childs[i].mouseX = mouseX;
				//_childs[i].mouseY = mouseY;
				//_childs[i].update(container);
				//if (mouseChild)
				//{
					//if (_childs[i].checkBitmapData())
					//{
						//_eventList.push(_childs[i]);
					//}
				//}
			//}
			//if (_frameIndex > _frameNum) _frameIndex = 0;
			//super.update(container);
			_frameIndex++;
			if (_frameIndex == 1) super.update(container);
			else _frameIndex = 0;
		}*/
		public function addActor(p_roleInfo:MRoleInfo):void
		{
			var tempActor:RoleDisplay = _rolePool.getObj();
			tempActor.roleInfo = p_roleInfo;
			tempActor.setAnimate(240, true, 2);
			tempActor.addEventListener(MMouseEvent.ROLL_OVER, actorRollOverHandler);
			tempActor.addEventListener(MMouseEvent.ROLL_OUT, actorRollOutHandler);
			tempActor.addEventListener(MMouseEvent.CLICK, actorClickHandler);
			tempActor.addEventListener(MMouseEvent.MOUSE_DOWN, actorMouseDownHandler);
			tempActor.addEventListener(MMouseEvent.MOUSE_UP, actorMouseUpHandler);
			tempActor.addEventListener(MMouseEvent.DOUBLE_CLICK, actorDoubleClickHandler);
			fs:for (var i:int = 1; i < 5; i++) 
			{
				tempActor.resLib[i] = [];
				
				ls:for (var j:int = 1; j < 8; j++) 
				{
					if (i == 1)
					{
						var bitmapData: BitmapData = Globals.lib.getResObj(Globals.domain.role + p_roleInfo.resId + "." + 2 + "-" + j);
						if (!bitmapData) break ls;
						var newBitmapData:BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0x00);
						var matrix:Matrix = new Matrix();
						matrix.scale( -1, 1);
						matrix.translate(bitmapData.width, 0);
						newBitmapData.draw(bitmapData, matrix);
						tempActor.resLib[i].push(newBitmapData);
					}else
					{
						bitmapData = Globals.lib.getResObj(Globals.domain.role + p_roleInfo.resId + "." + i + "-" + j);
						if (!bitmapData) break ls;
						tempActor.resLib[i].push(bitmapData);
					}
				}
				//print("长度=" + tempActor.resLib[i].length);
			}
			addChild(tempActor);
			//_roleChilds.push(tempActor);
			_roles.push(tempActor);
		}
		
		override protected function sort(p1:BaseDisplay, p2:BaseDisplay):int
		{
			return p1.y - p2.y;
		}

		private function actorDoubleClickHandler(e:MMouseEvent):void 
		{
			trace("双击" + e.currentTarget.roleInfo.soleId);
		}
		
		private function actorMouseUpHandler(e:MMouseEvent):void 
		{
			trace("松开鼠标" + e.currentTarget.roleInfo.soleId);
		}
		
		private function actorMouseDownHandler(e:MMouseEvent):void 
		{
			trace("按下鼠标" + e.currentTarget.roleInfo.soleId);
		}
		private function actorClickHandler(e:MMouseEvent):void 
		{
			trace("单击" + e.currentTarget.roleInfo.soleId);
		}
		
		private function actorRollOutHandler(e:MMouseEvent):void 
		{
			RoleDisplay(e.currentTarget).applyFilter();
		}
		
		private function actorRollOverHandler(e:MMouseEvent):void 
		{
			RoleDisplay(e.currentTarget).applyFilter(new GlowFilter());
		}
		
		
		public function delActor(p_soleId:int):void
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_roles[i].roleInfo.soleId == p_soleId)
				{
					_roles[i].dispose();
					_roles[i].removeEventListener(MMouseEvent.CLICK, actorClickHandler);
					if (_roles[i].parent) _roles[i].parent.removeChild(_roles[i]);
					_rolePool.dispose(_roles.splice(i, 1)[0]);
					break;
				}
			}
		}
	}

}